import {LogLanguage} from "../dist/index.js"

// 综合测试用例
const testCases = [
  // 标准格式
  "2024-01-15 10:30:45 [INFO] TID:abc123.def456.ghi789 [service.SBYBaseService:70] Processing request",
  
  // 没有TID:前缀的TID格式
  "2024-01-15 10:30:45 [INFO] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] [service.SBYBaseService:70] Processing request",
  
  // 混合格式
  "2024-01-15 10:30:45 [ERROR] TID:abc123 [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] [service.SBYBaseService:70] Error occurred",
  
  // 只有没有前缀的TID
  "2024-01-15 10:30:45 [WARNING] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] merId=1749611201012113",
  
  // 复杂格式
  "2024-01-15 10:30:45 [FATAL] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] (com.cn.yft.ora.bank.sby.service.SBYBaseService:70) Critical error",
  
  // 包含字符串和数字的格式
  "2024-01-15 10:30:45 [INFO] [fcdfba1a21214a8e97c7d8fd1a8aa287.1161.17544505317727189] Processing request with id=\"12345\" and count=100"
]

console.log("=== 综合功能测试 ===")

testCases.forEach((testCase, index) => {
  console.log(`\n测试 ${index + 1}:`)
  console.log(`输入: ${testCase}`)
  
  const tree = LogLanguage.parser.parse(testCase)
  let foundNodes = []
  
  tree.iterate({
    enter: (node) => {
      foundNodes.push({
        name: node.type.name,
        from: node.from,
        to: node.to,
        text: testCase.slice(node.from, node.to)
      })
    }
  })
  
  console.log("识别的节点:")
  foundNodes.forEach(node => {
    console.log(`  - ${node.name}: "${node.text}" (${node.from}-${node.to})`)
  })
  
  // 检查各种类型的节点
  const dateNodes = foundNodes.filter(node => node.name === 'DATEExpression')
  const logLevelNodes = foundNodes.filter(node => 
    node.name.includes('Expression') && 
    (node.name.includes('FATAL') || node.name.includes('ERROR') || 
     node.name.includes('INFO') || node.name.includes('WARNING') || 
     node.name.includes('Debug'))
  )
  const classPathNodes = foundNodes.filter(node => node.name === 'CLASSPATHExpression')
  const tidNodes = foundNodes.filter(node => node.name === 'TIDExpression')
  const stringNodes = foundNodes.filter(node => node.name === 'StringLiteral')
  const numberNodes = foundNodes.filter(node => node.name === 'NumberExpression')
  
  console.log("\n功能检查:")
  console.log(`  - 日期识别: ${dateNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 日志级别识别: ${logLevelNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 类路径识别: ${classPathNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - TID识别: ${tidNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 字符串识别: ${stringNodes.length > 0 ? '✓' : '✗'}`)
  console.log(`  - 数字识别: ${numberNodes.length > 0 ? '✓' : '✗'}`)
  
  // 检查没有TID:前缀的TID格式
  const tidRegex = /\[([a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*(\.[0-9]+)*)\]/g
  let match
  let foundTIDsWithoutPrefix = []
  
  while ((match = tidRegex.exec(testCase)) !== null) {
    const content = match[1]
    if (!content.startsWith('TID:') && !content.startsWith('tid:')) {
      const isLogLevel = /^(FATAL|ERROR|INFO|WARNING|DEBUG|WARN)$/i.test(content)
      if (!isLogLevel) {
        foundTIDsWithoutPrefix.push(match[0])
      }
    }
  }
  
  if (foundTIDsWithoutPrefix.length > 0) {
    console.log(`  - 无前缀TID识别: ✓ (${foundTIDsWithoutPrefix.join(', ')})`)
  } else {
    console.log(`  - 无前缀TID识别: ✗`)
  }
})
